<?php

	class wallMaintainance
	{		
		public static function clearFiles($all = false)
		{
			$baseDir = "tmp_dir/Wall2/";
			if (!file_exists($baseDir) && !is_dir($baseDir))
				mkdir($baseDir);
				
			$handle = opendir($baseDir);
			
			$filesDeleted = 0;
			
			//remove files older than 48h
		    $tsToRemove = 48 * 60 * 60;
		        
		    while (false !== ($entry = readdir($handle))) 
		    {
		        if ($entry== "." || $entry == "..") continue;
		        
		        if ($all)
		        {
		        	$filesDeleted++;
		        	unlink($baseDir.$entry);
		        	continue;
		        }
		        
		        //cycle files
		        $fTime = filemtime($baseDir.$entry);
		        if ($fTime < time() - $tsToRemove )
		        {
		        	$filesDeleted++;
		        	unlink($baseDir.$entry);
		        }
		    }
		    
		    closedir($handle);
		    
		    $arRet = array();
		    $arRet["resp"] = true;
		    $arRet["filesRemoved"] = $filesDeleted;
		    
		    return $arRet;
		}
		
		public static function parseXML($forceAll = false)
		{
			if (!class_exists("xmlsOb"))
			{
				echo "failed";
				exit;
			}
			
			//chk versions
			$b = new Bot();
			$curVer = $b->GetCurrentRevision();
			unset($b);
			
			$db = wallDB::getInstance();
			
			$wallVersion = $db->getParsedVersion();
			
			$arVirals = $db->getAllVirals();
			
			if (($curVer != $wallVersion) || $forceAll)
			{
				
				$xmlsOb = new xmlsOb();
				$inXML = 0;
				$added = 0;
				foreach ($xmlsOb->gsXML->virals->viral as $item)
				{
					$name                 = (string)$item['name'];
					
					//skip invalid links
					if ($name=="campaign_payer" || $name=="m_rsvp")
						continue;
					
					$inXML++;
					//chk if exists
					if (in_array($name, $arVirals))
						continue;
					
					$timeTillReset        = intval( (string)$item->timeTillReset ); 
					$maxHostRewards       = intval( (string)$item->reward->expirationTime );
					$rewardexpirationTime = intval( (string)$item->reward->expirationTime );
					$rewardmaxHelpers     = intval( (string)$item->reward->maxHelpers );
					$hostRewardsType      = (string)$item->reward->hostRewards->reward['type'];
					$hostRewardsAmount    = intval( (string)$item->reward->hostRewards->reward->data['amount'] );
					$helperRewardsType    = (string)$item->reward->helperRewards->reward['type'];
					$helperRewardsAmount  = intval( (string)$item->reward->helperRewards->reward->data['amount'] );
					
					//image
					$hostRewardsType      = (string)$item->reward->hostRewards->reward['type'];
					$hostRewardItemName   = (string)$item->reward->hostRewards->reward->data["itemName"];
					
					//get imPath
					$itm = $xmlsOb->gsXML->xpath("items/item[@name='".$hostRewardItemName."']");
					$imPath = (string)$itm[0]->image["url"];
					
					$ssql = "REPLACE INTO virals VALUES (
								'$name',
								$timeTillReset,
								$maxHostRewards,
								$rewardexpirationTime,
								$rewardmaxHelpers,
								'$imPath',
								'$hostRewardsType',
								$hostRewardsAmount,
								'$helperRewardsType',
								$helperRewardsAmount,
								$curVer
							)";
					$db->query($ssql);
					$added++;
				}
				
				$db->setParsedVersion($curVer);
				$db->setSetting('lastDateChecked', time());
			}
			
			$arRet = array();
			$arRet["resp"] = true;
			$arRet["inDB"] = count($arVirals);
			unset($arVirals);
			$arRet["added"] = $added;
			$arRet["inXML"] = $inXML;
			
			return $arRet;
		}
	}

?>